﻿using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace PharmacyRepositoy
{
    public class ClienteRepository : MasterRepository, IClienteRepository
    {
        public IEnumerable<PharmacyBussinesEntities.Cliente> GetClientesByCriterio(string criterio)
        {
            var query = from p in Context.Clientes
                select p;
            if (!string.IsNullOrEmpty(criterio))
            {
                query = from p in query
                    where p.Nombres.ToUpper().Contains(criterio.ToUpper())
                          || p.Apellidos.ToUpper().Contains(criterio.ToUpper())
                          || p.DNI.ToUpper().Contains(criterio.ToUpper())
                          || p.Direccion.ToUpper().Contains(criterio.ToUpper())
                    select p;
            }
            return query;
        }

        public PharmacyBussinesEntities.Cliente GetClienteById(int id)
        {
            var query = from p in Context.Clientes
                select p;
            if (id!=null)
            {
                query = from p in query
                    where p.Id.Equals(id)
                    select p;
            }
            return query.SingleOrDefault();
        }

        public void UpdateCliente(PharmacyBussinesEntities.Cliente cliente)
        {
            Context.Clientes.Attach(cliente);
            Context.Entry(cliente).State = EntityState.Modified;
            Context.SaveChanges();
        }


        public void RemoveCliente(Int32 id)
        {
            var exist = Context.Clientes.Find(id);
            if (exist != null)
            {
                Context.Clientes.Remove(exist);
                Context.SaveChanges();
            }
        }


        public void AddCliente(PharmacyBussinesEntities.Cliente cliennte)
        {
            Context.Clientes.Add(cliennte);
            Context.SaveChanges();
        }


        public PharmacyBussinesEntities.Cliente GetClienteByRucDNI(string rucDNI)
        {
            return Context.Clientes.Where(p => p.DNI.Equals(rucDNI)).SingleOrDefault();
        }
    }
}
